<?php
namespace app\admin\controller;

use app\model\admin;
use think\Db;
use think\Config;
use think\Request;
use think\Session;
use think\Controller;

class System extends Controller
{
    //菜单管理
    public function navigation()
    {
        $dao = new \app\model\admin\System();
        $lists = $dao->navigation_list('',$sort='sort desc',$paginate=15);//查询前台网站菜单
        $this->assign('lists', $lists);
        return $this->fetch('system/navigation');
    }

    //菜单详情
    public function navigation_detail()
    {
        $dao = new \app\model\admin\System();
        $id = $request = $this->request->get('id');
        if(empty($id)){
            $data = array();
        }else{
            $data = $dao->navigation_info($id);
        }
        return $this->fetch('system/navigation_detail',$data);
    }

    //菜单管理
    public function navigation_status()
    {
        $dao = new \app\model\admin\System();
        if($this->request->get("id")<1){
            return json(['status'=>2,'data'=>(object)[],'msg'=>'参数错误!']);
        }else{
            $id = $this->request->get("id");
        }
        $status = $this->request->get("status");
        if(!in_array($status,[1,2]))
        {
            return json(['status'=>2,'data'=>(object)[],'msg'=>'参数错误!']);
        }
        $where['id'] = $id;
        $data['status'] = $status;
        $status = $dao->update_navigation($where,$data);
        if($status > 0){
            return json(['status'=>0,'data'=>(object)[],'msg'=>'操作成功!']);
        }else{
            return json(['status'=>3,'data'=>(object)[],'msg'=>'操作失败!']);
        }
    }
    //菜单内页管理
    public function navigation_inside()
    {
        $dao = new \app\model\admin\System();
        if($this->request->get("id")<1){
            return json(['status'=>2,'data'=>(object)[],'msg'=>'参数错误!']);
        }else{
            $id = $this->request->get("id");
        }
        $status = $this->request->get("status");
        if(!in_array($status,[1,2]))
        {
            return json(['status'=>2,'data'=>(object)[],'msg'=>'参数错误!']);
        }
        $where['id'] = $id;
        $data['inside'] = $status;
        $status = $dao->update_navigation($where,$data);
        if($status > 0){
            return json(['status'=>0,'data'=>(object)[],'msg'=>'操作成功!']);
        }else{
            return json(['status'=>3,'data'=>(object)[],'msg'=>'操作失败!']);
        }
    }

    //保存修改
    public function navigation_save()
    {
        $dao = new \app\model\admin\System();
        $data['id'] = deletehtml($this->request->post('id'));
        $data['title'] = deletehtml($this->request->post('title'));
        $data['alt_title'] = deletehtml($this->request->post('alt_title'));
        $data['url'] = deletehtml($this->request->post('url'));
        $data['follow'] = deletehtml($this->request->post('follow'));
        $data['status'] = $this->request->post('status');
        $data['sort'] = $this->request->post('sort');

        if(empty($data['id'])){
            $navigation_status = $dao->add_navigation($data);
        }else{
            $where['id'] = $data['id'];
            $navigation_status = $dao->update_navigation($where,$data);
        }
        if($navigation_status){
            $this->redirect('/admin/system/navigation?');
        }else{
            $this->error('操作失败！', '/admin/system/navigation_detail?id='.$data['id']);
        }
    }

    //删除导航栏
    public function navigation_delete()
    {
        $dao = new \app\model\admin\System();
        if($this->request->get("id")<1){
            return json(['status'=>2,'data'=>(object)[],'msg'=>'参数错误!']);
        }else{
            $id = $this->request->get("id");
        }
        $advert_delete = $dao->delete_navigation([$id]);
        if($advert_delete == 0){
            return json(['status'=>3,'data'=>(object)[],'msg'=>'删除失败!']);
        }else{
            return json(['status'=>0,'data'=>$advert_delete,'msg'=>'操作成功!']);
        }
    }

    //修改网站关键词、描述等信息
    public function web_info()
    {
        $dao = new \app\model\admin\System();
        if (Request::instance()->isPOST()){
            $data['web_title'] = deletehtml($request = $this->request->post('web_title'));
            $data['web_keywords'] = deletehtml($request = $this->request->post('web_keywords'));
            $data['web_descript'] = deletehtml($request = $this->request->post('web_descript'));
            $data['company'] = deletehtml($this->request->post('company'));
            $data['name'] = deletehtml($this->request->post('name'));
            $data['weixin'] = deletehtml($this->request->post('weixin'));
            $data['qq'] = deletehtml($this->request->post('qq'));
            $data['phone'] = deletehtml($this->request->post('phone'));
            $data['seo_title'] = deletehtml($request = $this->request->post('seo_title'));
            $data['tongji'] = $request = $this->request->post('tongji');
            $data['baidu_token'] = $request = $this->request->post('baidu_token');
            $file = $this->request->file('image');
            $status = $dao->save_web_info($data,$file);//修改网站信息
            if($status) $this->redirect('/admin/system/web_info?');
            else $this->error('修改失败！', 'System/web_info');
        }
        $data = $dao->web_info();//查询网站信息
        return $this->fetch('system/web_info',$data);
    }

    //修改当前管理员信息
    public function admin_info()
    {
        $dao = new \app\model\admin\System();
        if (Request::instance()->isPOST()){
            $id = Session::get('admin_info.id');
            $password = deletehtml($request = $this->request->post('password'));
            $new_password = deletehtml($request = $this->request->post('new_password'));
            $repeat_password = deletehtml($request = $this->request->post('repeat_password'));
            if(empty($new_password))$this->error('新密码不能为空！', 'System/admin_info');
            if($new_password!==$repeat_password){
                $this->error('新密码不一致！', 'System/admin_info');
            }
            $status = $dao->checked_admin_info($id,$password);//验证管理员密码
            if(!$status)$this->error('旧密码错误！', 'System/admin_info');

            $where['id'] = $id;
            $where['password'] = $password;
            $status = $dao->update_admin_info($id,$new_password);//修改管理员密码
            if(!$status){
                $this->error('密码修改失败！', 'System/admin_info');
            }else{
                $this->success('修改成功', 'System/admin_info');
            }
        }
        return $this->fetch('system/admin_info');
    }

    //读取登录日志
    public function login_log(){
        //排除最新登录的一条ip
        $new_ip = Db::name('admin_login_log')->where('user_id',Session::get('admin_info.id'))->limit(1)->order('id desc')->select();
        if(Session::get('admin_info.powers')=='all'){
            $login_list = Db::name('admin_login_log')->where('id','not in',$new_ip['id'])->select();
        }else{
            $login_list = Db::name('admin_login_log')
                ->where('id','not in',$new_ip[0]['id'])
                ->where('user_id',Session::get('admin_info.id'))
                ->order('id desc')
                ->select();
        }
        if(!empty($login_list)){
            //时间戳转时间（也可在查询的时候用sql转换）
            foreach($login_list as $val){
                $val['login_time'] = date("Y-m-d H:i:s",$val['login_time']);
                $lists[] = $val;
            }
            $data['login_list'] = $lists;
            $data['now_ip'] = getIP();
            return json(['status'=>0,'data'=>$data,'msg'=>'操作成功!']);
        }else{
            return json(['status'=>1,'data'=>(object)[],'msg'=>'暂无记录!']);
        }

    }

    //查询文章和留言数量
    public function article_num(){
        $a_where['status'] = 1;//文章正常状态
        $a_dao = new \app\model\admin\Article();
        $count['article'] = $a_dao->get_article_num($a_where);
        $m_where = '';//文章正常状态
        $m_dao = new \app\model\admin\Message();
        $count['message'] = $m_dao->get_message_num($m_where);
        return json(['status'=>0,'data'=>$count,'msg'=>'ok!']);

    }

    //查询网站收录量
    public function web_view_num(){
        $dao = new \app\model\admin\System();
        $get_view_count = $dao->web_view(strtotime(date("Y-m-d",time())));
        if($get_view_count>1){
            $get_view_count = two_array_unique($get_view_count,'ip');
        }
        return json(['status'=>0,'data'=>count($get_view_count),'msg'=>'ok']);
    }

    //登录滑动验证
    public function login_token(){
        $this->request->token('login_token');
        return json(['status'=>0,'data'=>(object)[],'msg'=>'ok']);
    }
}


